package org.manusoft.refill.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.manusoft.refill.domain.MyLazyVehicleDataModel;
import org.manusoft.refill.domain.TVehicleDetailsRf;
import org.manusoft.refill.service.VehicleService;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("manageVehicles")
@Scope("session")
public class ManageVehicles implements Serializable {

    private LazyDataModel<TVehicleDetailsRf> lazyDataModel;
    private VehicleService vehicleService;
//    private String title;
    private String regNum;
    private TVehicleDetailsRf[] selectedVehicles;

    @Autowired
    public ManageVehicles(VehicleService vehicleService) {
    	System.out.println("created movies");
        this.vehicleService = vehicleService;
        lazyDataModel = new MyLazyVehicleDataModel(vehicleService);
    }

    public LazyDataModel<TVehicleDetailsRf> getVehicles() {
        return lazyDataModel;
    }

    public void setVehicles(LazyDataModel<TVehicleDetailsRf> movies) {
        this.lazyDataModel = movies;
    }

    public TVehicleDetailsRf[] getSelectedVehicles() {
        return selectedVehicles;
    }

    public void setSelectedVehicles(TVehicleDetailsRf[] selectedVehicles) {
        this.selectedVehicles = selectedVehicles;
    }

    public List<String> getVehiclesByRegNum(String regNum) {
        List<TVehicleDetailsRf> foundVehicles = vehicleService.findByRegNumAutoComplete(regNum);
        List<String> titles = new ArrayList<String>();

        for (TVehicleDetailsRf m : foundVehicles) {
            titles.add(m.getVehCharRegno());
        }

        return titles;
    }

    public void search(ActionEvent actionEvent) {
    	lazyDataModel.load(0,1, null, SortOrder.UNSORTED, new HashMap<String, String>() {
            {
                put("vehCharRegno", regNum);
            }
        });
    }

	/**
	 * @return the regNum
	 */
	public String getRegNum() {
		return regNum;
	}

	/**
	 * @param regNum the regNum to set
	 */
	public void setRegNum(String regNum) {
		this.regNum = regNum;
	}
}